﻿using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace PM.Code.Web
{
    /// <summary>
    ///将数据文件导入到Sql Server数据库中
    /// </summary>
    public class input
    {
        public input()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// 将dataset中的内容导入到Sql中（第一行是标头）
        /// </summary>
        /// <param name="ds">DataSet控件</param>
        /// <param name="tablename">插入的表名</param>
        /// <param name="fields">字段名(a,b,c,……)</param>
        /// <returns>返回执行结果string</returns>
        public static int DataSetToSql(DataSet ds, string tablename, string fields)
        {
            List<string> sqlstr = new List<string>();
            //SqlConnection _con = new SqlConnection(PM.Code.DAL.DbHelper.connectionString);
            //SqlCommand cmd = new SqlCommand();
            //cmd.Connection = _con;
            StringBuilder sb = new StringBuilder();
            if (ds.Tables[0].Rows.Count > 1)
            {
                for (int i = 1; i <= ds.Tables[0].Rows.Count; i++)
                {
                    sb.Append("INSERT INTO ").Append(tablename).Append("(").Append(fields).Append("builddate)").Append("  VALUES('");
                    for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                        sb.Append(ds.Tables[0].Rows[i].ItemArray[j].ToString() + "','");
                    sb.Append(",getdate())");
                    sqlstr.Add(sb.ToString());
                    sb.Remove(0, sb.Length);
                    //cmd.CommandText = sb.ToString();
                }
            }
            return PM.Code.DAL.DbHelper.ExecuteSqlTran(sqlstr);
            //_con.Open();
            //int j = cmd.ExecuteNonQuery();
            //_con.Close();
        }
    }
}

